<?php
class graficoc24h extends Controller 
{
	function __construct()
	{
		parent::Controller();
		$this->load->database();
		$this->load->model(array('mdisponible','mpresupuesto','mcompania'));
	
	}
	
	
	
	function index()
	{
		$this->load->view('main', array('content_page' => 'grafico24h',
		                                'menutop' => 5,
		                                'var3'=>$this->venta(),
		                                'var2'=>$this->ventaExple()));
	}
	
	
	
 function semana($sem) 
 {

 	          for ($i = 0;$i < 365; $i++ )
              {
	            $dia[] = date( "Y-m-d-W",mktime(0,0,0,date('m'),date('d')-$i,date('Y')) );
              }


             foreach ($dia as $d)
             {
  	            $dia = explode('-',$d);
  	            $semana[$dia[3]][] = $dia[0].'-'.$dia[1].'-'.$dia[2];
              }
                   $semana = $semana[$sem];
            
            
  	 return $semana;
  	   
  	
 }   
	

   function encodeDataURL($strDataURL, $addNoCacheStr=false) {
            if ($addNoCacheStr==true) {
		    if (strpos(strDataURL,"?")<>0)
			  $strDataURL .= "&FCCurrTime=" . Date("H_i_s");
		     else
			   $strDataURL .= "?FCCurrTime=" . Date("H_i_s");
             }

	        return urlencode($strDataURL);
	
             }

    function datePart($mask, $dateTimeStr) {
             @list($datePt, $timePt) = explode(" ", $dateTimeStr);
            $arDatePt = explode("-", $datePt);
            $dataStr = "";
 
    if (count($arDatePt) == 1) {
        list($year, $month, $day) = $arDatePt;
        
        switch ($mask) {
        case "m": return $month;
        case "d": return $day;
        case "y": return $year;
        }
      
        return (trim($month . "/" . $day . "/" . $year));
    }
    return $dataStr;
}

	public function data()
	{
	    $page = $this->input->post('page') ? $this->input->post('page') : 1;
		$sortname = $this->input->post('sidx') ? $this->input->post('sidx') : $this->input->post('sidx');
		$sortorder = $this->input->post('sord') ? $this->input->post('sord') : 'asc';
		$limit = $this->input->post('rows') ? $this->input->post('rows') : 10;

		$start = $limit * $page - $limit;
		if($start < 0) $start = 0;

		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
		header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
		header("Cache-Control: no-cache, must-revalidate" );
		header("Pragma: no-cache" );

        $records = $this->mpresupuesto->loadPresupuesto($start, $limit, $sortname, $sortorder);	
        
        $records = array_reverse($records);
        
        $totalRecords = count($records);
        
        $totalPages = ceil($totalRecords/$limit);
		if ($page > $totalPages) $page = $totalPages;

		$json_output = array(
		'page'		=> $page,
		'total'		=> $totalPages,
		'records'	=> $totalRecords,
		'rows'		=> array()
		);
		
		if ($totalRecords > 0) {
			
		    foreach ($records as $k)
		    {
		      $disponible =	$this->mdisponible->loadRegistroGrafico($k->getAnos(),$k->getWk()); 
		      $disponible = $disponible->getDisponible();
		     	
		       $arr = $json_output['rows'][] = array(
				    'id'=>$k->getId(),
				    'anos'=>$k->getAnos(),
				    'wk' => $k->getWk(),
				    'presupuesto'=>$k->getPresupuesto(),
		            'diponible'=>$disponible,
		            'resultado'=>$disponible-$k->getPresupuesto()
		        );
		    }
			
		}
		
		$a=json_encode($json_output);
		$a=str_replace('\\','',$a);
		echo $a;
		
	}

function renderChart($chartSWF, $strURL, $strXML, $chartId, $chartWidth, $chartHeight, $debugMode, $registerWithJS) {

	if ($strXML=="")
        $tempData = "//Set the dataURL of the chart\n\t\tchart_$chartId.setDataURL(\"$strURL\")";
    else
        $tempData = "//Provide entire XML data using dataXML method\n\t\tchart_$chartId.setDataXML(\"$strXML\")";


    $chartIdDiv = $chartId . "Div";
    $ndebugMode = $this->boolToNum($debugMode);
    $nregisterWithJS = $this->boolToNum($registerWithJS);

$render_chart = <<<RENDERCHART

	<!-- START Script Block for Chart $chartId -->
	<div id="$chartIdDiv" align="center">
		Chart.
	</div>
	<script type="text/javascript">	
		//Instantiate the Chart	
		var chart_$chartId = new FusionCharts("$chartSWF", "$chartId", "$chartWidth", "$chartHeight", "$ndebugMode", "$nregisterWithJS");
		$tempData
		//Finally, render the chart.
		chart_$chartId.render("$chartIdDiv");
	</script>	
	<!-- END Script Block for Chart $chartId -->
RENDERCHART;

  return $render_chart;
}


 function boolToNum($bVal) {
                 return (($bVal==true) ? 1 : 0);
   }



function venta() {
   
	$intYear = 1996;
	$addJSLinks = true;
	$forDataURL= false;
	$ors['CategoryID'] = 20;

	      $strCat = "<categories>";

	           $strAmtDS = "<dataset seriesname='Presupuesto'>";
          	    $strQtyDS = "<dataset seriesName='Disponible'>";
          	     $strVntDS = "<dataset seriesName='Ventas'>";
                     $presupuesto = $this->mpresupuesto->loadGrafico();
                     
                     $array = array_reverse($presupuesto);
          	         foreach ($array as $k){
          	         	
          	         	$ventaExpre = $this->semana($k->getWk());
          	         	
          	         	$cantidad = $this->mcompania->loadCantidadExples($ventaExpre[0],$ventaExpre[count($ventaExpre)-1]);
          	         	
          	         	 $disponible = $this->mdisponible->loadRegistroGrafico($k->getAnos(),$k->getWk());
          	         	
          	         	 $strCat .= "<category label='".$k->getAnos()."/".$k->getWk()."'/>";
                         $strAmtDS .= "<set value='".$k->getPresupuesto()."' />";
                         $strQtyDS .= "<set value='".$disponible->getDisponible()."'/>";
                         $strVntDS .= "<set value='".$cantidad."'/>";
          	         	
          	         	
          	         }     
          	    
          	         
              
	                	
	                	
  
             
	$strCat .= "</categories>";
	
	$strAmtDS .= "</dataset>";
	$strQtyDS .= "</dataset>";
	$strVntDS .= "</dataset>";
	

	$strXML2 = $strCat . $strAmtDS . $strQtyDS . $strVntDS;
	
	$strXML  = "<chart caption='Compa&ntilde;ias 24H' palette='' animation='' formatNumberScale='0' PYAXisName='True' SYAxisName='' numberPrefix='' labeldisplay='STAGGER' seriesNameInToolTip='0' showValues='0' plotSpacePercent='0'>";
    $strXML .=$strXML2;
    $strXML .= "<styles><definition><style type='font' name='CaptionFont' size='15' color='' /><style type='font' name='SubCaptionFont' bold='0' /></definition><application><apply toObject='caption' styles='CaptionFont' /><apply toObject='SubCaption' styles='SubCaptionFont' /></application></styles>";
	$strXML .= "</chart>";
	

	return $this->renderChart(base_url()."public/swf_charts/MSColumn3DLineDY.swf", "",$strXML, "CatCum3", 750,300, false, false	);
	
}


function ventaExple() {
   
	$intYear = 1996;
	$addJSLinks = true;
	$forDataURL= false;
	$ors['CategoryID'] = 21;

	      $strCat = "<categories>";

	           $strAmtDS = "<dataset seriesname='Venta'>";
   
              
          	      
               for ($j = 0;$j < 10;$j++){

                          
          	         	
          	         	 $strCat .= "<category label='hola'/>";
                         $strAmtDS .= "<set value='2' />";
               	
               }      
             
	$strCat .= "</categories>";
	
	$strAmtDS .= "</dataset>";
	 

	$strXML2 = $strCat . $strAmtDS;
	
	$strXML  = "<chart caption='Ventas Ex' palette='' animation='' formatNumberScale='0' PYAXisName='True' SYAxisName='' numberPrefix='' labeldisplay='STAGGER' seriesNameInToolTip='0' showValues='0' plotSpacePercent='0'>";
    $strXML .=$strXML2;
    $strXML .= "<styles><definition><style type='font' name='CaptionFont' size='15' color='' /><style type='font' name='SubCaptionFont' bold='0' /></definition><application><apply toObject='caption' styles='CaptionFont' /><apply toObject='SubCaption' styles='SubCaptionFont' /></application></styles>";
	$strXML .= "</chart>";
	

	return $this->renderChart(base_url()."public/swf_charts/MSColumn3DLineDY.swf", "",$strXML, "CatCum", 750,300, false, false	);
	
}

 
   function ultimo($array)
   {
	   $dia = $array[count($array)-1].'+'.$array[0];
	   return $dia;
   } 



}
?>